<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-form">
        <h2>登录</h2>
        <a-form :model="loginForm" @finish="handleFinish">
          <div>
            <a-form-item
              name="username"
              :rules="[{ required: true, message: '请输入用户名/邮箱/手机号' }]"
            >
              <a-input
                v-model:value="loginForm.username"
                placeholder="用户名/邮箱/手机号"
              />
            </a-form-item>

            <a-form-item
              name="password"
              :rules="[{ required: true, message: '请输入密码' }]"
            >
              <a-input-password
                v-model:value="loginForm.password"
                placeholder="密码"
              />
            </a-form-item>
          </div>
          <div class="form-footer">
            <a class="forgot-password" @click="forgotPassword">忘记密码?</a>
            <a-button type="primary" html-type="submit" class="login-button">
              登录
            </a-button>
          </div>
        </a-form>
      </div>
      <div class="register-box">
        <h3 style="font-size: 35px">没有帐号?</h3>
        <p style="margin-top: 30px">立即注册吧 😊</p>
        <a-button class="register-button" @click="goToRegister">
          注册
        </a-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive } from "vue";
import { useRouter } from "vue-router";
import { useAuthStore } from "@/store/auth";

const router = useRouter();
const authStore = useAuthStore();
const loginForm = reactive({
  username: "",
  password: "",
});

const handleFinish = async (values: any) => {
  try {
    // 使用 auth store 的 login 方法
    const success = await authStore.login(
      loginForm.username,
      loginForm.password
    );

    if (success) {
      console.log("登录成功");
      router.push("/home"); // 或者改为 router.push("/chat")
    } else {
      alert("用户名或密码错误");
    }
  } catch (error) {
    console.error("登录请求失败", error);
    alert("登录请求失败，请稍后再试");
  }
};

const forgotPassword = () => {
  router.push("/forgot-password");
};

const goToRegister = () => {
  router.push("/register");
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-image: url("@/assets/images/screen.png");
  background-size: cover;
  /* background: linear-gradient(135deg, #f5f7fa 0%, #c2c9d4 100%); */
  padding: 20px;
}

.login-box {
  display: flex;
  border-radius: 15px;
  overflow: hidden;
  width: 800px;
  min-height: 500px; /* 增加最小高度 */
  backdrop-filter: blur(5px);
  box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
  border: 1px solid rgba(255, 255, 255, 0.18);
}

.login-form {
  flex: 1;
  padding: 120px 50px 50px 50px;
  background: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(5px);
}

.register-box {
  flex: 1;
  /* background: rgba(255, 77, 79, 0.8); */
  background: linear-gradient(
    135deg,
    rgba(242, 110, 112, 0.9) 0%,
    rgba(255, 19, 101, 0.9) 100%
  );
  backdrop-filter: blur(7px);
  color: white;
  padding: 50px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

h2 {
  margin-bottom: 10px;
  color: #333;
  font-size: 28px;
}

/* h3 {
  margin-bottom: 30px;
  color: #ffffff;
  font-size: 30px;
} */

.form-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 24px;
}

.forgot-password {
  color: #666;
  transition: color 0.3s;
}

.forgot-password:hover {
  color: #ff4d4f;
}

.login-button {
  background: #ff4d4f;
  border-color: #ff4d4f;
  width: 120px;
  height: 40px;
  font-size: 16px;
}

.login-button:hover {
  background: #ff7875;
  border-color: #ff7875;
}

.register-button {
  background: transparent;
  border: 2px solid white;
  color: white;
  margin-top: 20px;
  height: 40px;
  width: 140px;
  font-size: 16px;
  transition: all 0.3s;
}

.register-button:hover {
  background: rgba(255, 255, 255, 0.2);
  border-color: white;
  color: white;
}

.register-box h3 {
  color: white;
  margin-bottom: 12px;
  font-size: 24px;
}

.register-box p {
  color: white;
  margin-bottom: 24px;
  font-size: 16px;
}

:deep(.ant-input),
:deep(.ant-input-password) {
  height: 40px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.9);
}

:deep(.ant-input-password .ant-input) {
  height: 38px;
  background: transparent;
}

:deep(.ant-input-password-icon) {
  color: rgba(0, 0, 0, 0.45);
  margin-top: -2px;
}

:deep(.ant-form-item) {
  margin-bottom: 24px;
}

@media (max-width: 768px) {
  .login-box {
    flex-direction: column;
    width: 100%;
    max-width: 500px;
  }

  .login-form,
  .register-box {
    width: 100%;
    padding: 30px;
  }
}
</style>
